<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Think\Model;

/**
 * 报损管理控制器
 */
class LossController extends AdminController {
    public function index(){
        $lossModel=new Model();
        $sql="select left(warn_batch,8) as warn_batch,COUNT(DISTINCT BIN) lossclassify,sum(cnt) loss_cnt,count(DISTINCT lossNumber) losstime  from gr_inventory_losslist group by left(warn_batch,8) order by left(warn_batch,8) desc";
        $res=$lossModel->query($sql);
        $total = count($res);
        if (isset($REQUEST['r'])) {
            $listRows = (int) $REQUEST['r'];
        } else {
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $this->assign('_total', $total);
        $sql.= " limit $page->firstRow,$page->listRows";
        $res=$lossModel->query($sql);
        foreach($res as $k=>$v){
            $subSql="select (select ifnull(sum(a.spec*b.cnt),0) from gr_material_product a,gr_inventory_inlist b where a.BIN=b.BIN and b.BIN like '1%' and b.warn_batch like '".$v['warn_batch']."%') as in_cnt,
                    (select ifnull(sum(a.spec*b.cnt),0) from gr_material_product a,gr_inventory_outlist b where a.BIN=b.BIN and b.BIN like '1%' and b.warn_batch like '".$v['warn_batch']."%') as out_cnt
                    from gr_inventory_losslist limit 1";
            $subRes=$lossModel->query($subSql);
            $res[$k]['in_cnt']=$subRes[0]['in_cnt'];
            $res[$k]['out_cnt']=$subRes[0]['out_cnt'];
            $res[$k]['loss_in']=number_format($v['loss_cnt']*100/$subRes[0]['in_cnt']);
            $res[$k]['loss_out']=number_format($v['loss_cnt']*100/$subRes[0]['out_cnt']);
            $res[$k]['out_in']=number_format($subRes[0]['out_cnt']*100/$subRes[0]['in_cnt']);

        }
        $this->assign('_list',$res);
        $this->title="批次报损数据";
        $this->display();
    }
    public function batchdetail($warn_batch='',$export=0){
        $sql="select * from gr_inventory_warn where warn_batch like '".$warn_batch."%' and BIN like '1%' ";
        $model=new Model();
        $res=$model->query($sql);
        $data=[];
        /**
         *分页
        $total = count($res);
        if (isset($REQUEST['r'])) {
            $listRows = (int) $REQUEST['r'];
        } else {
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $this->assign('_total', $total);
        $sql.= " limit $page->firstRow,$page->listRows";
        $res=$model->query($sql);
         *
         **/
        //foreach 循环取出每个商品的报损数局
        $lossModel=M('Inventory_losslist');
        foreach($res as $k=>$v){
            $losssql="select sum(cnt) loss_cnt,unit from gr_inventory_losslist where BIN like '_".substr($v['BIN'],1,4)."__' and warn_batch = '".$v['warn_batch']."'";
            $lossres=$lossModel->query($losssql);
            $spec=get_name_by_id($v['BIN'],'Material_product','BIN','spec');
            $specNum=spec_number_format($spec);
            $totalProductNum=$specNum*$v['initcnt'];
            $res[$k]['pname']=get_name_by_id($v['BIN'],'Material_product','BIN','pname');
            $res[$k]['spec']=get_name_by_id($v['BIN'],'Material_product','BIN','spec');
            $res[$k]['loss_cnt']=$lossres[0]['loss_cnt'];
            //总量ss
            $res[$k]['totalProductNum']=$totalProductNum;
            $inventoryProductNum=$specNum*$v['cnt'];
            //库存量
            $res[$k]['inventoryProductNum']=$inventoryProductNum;
            $outProductNum=$specNum*($v['initcnt']-$v['cnt']);

            $data[$k]['BIN']=$res[$k]['BIN'];
            $data[$k]['pname']=$res[$k]['pname'];
            $data[$k]['spec']=$res[$k]['spec'];
            $data[$k]['loss_cnt']=$lossres[0]['loss_cnt'];
            $data[$k]['in_cnt']=$totalProductNum;
            $data[$k]['loss_in']=number_format($lossres[0]['loss_cnt']*100/$totalProductNum)."%";
            $data[$k]['out_cnt']=$outProductNum;
            $data[$k]['loss_out']=number_format($lossres[0]['loss_cnt']*100/$outProductNum)."%";
            $data[$k]['inventoryProductNum']=$inventoryProductNum;
        }
        $data=multi_array_sort($data,'loss_cnt');
        if($export==0){
            $this->assign('_list',$data);
            $this->title="批次报损明细[".date('Y-m-d',strtotime($warn_batch))."期批次]";
            $this->display();
        }else{
            if ($data){
                //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
                import("Org.Util.PHPExcel");
                import("Org.Util.PHPExcel.Writer.Excel5");
                import("Org.Util.PHPExcel.IOFactory.php");

                $filename = "output_order_excel";

                $headArr = array("BIN","商品名称","商品规格","报损数量","入库量","入库报损率","出库量","出库报损率","库存量");

                $title = "实时库存列表";
                $this->getExcel($filename, $headArr, $data, $title);
            }else{
                $this->error("没有可导出的数据！");
            }
        }
    }
    public function batchclass($warn_batch=''){
        $model=new Model();
        $sql="select sum(cnt) cnt,LEFT(BIN,1) as class_no,(select sum(cnt) from gr_inventory_losslist  where warn_batch like '".$warn_batch."%') as total from gr_inventory_losslist  where warn_batch like '".$warn_batch."%' group by LEFT(BIN,1)";
        $res=$model->query($sql);
        $this->_classRes=$res;
        $this->classRes=json_encode($res);
        $this->display('dailyclassdetail');
    }
    public function batchreason($warn_batch=''){
        $model=new Model();
        $reasonSql="select sum(cnt) as reasonCnt,reason,(select sum(cnt) from gr_inventory_losslist  where warn_batch like '".$warn_batch."%')  as total from gr_inventory_losslist  where warn_batch like '".$warn_batch."%' group by reason";
        $reasonRes=$model->query($reasonSql);
        $typeSql="select sum(cnt) as typeCnt,loss_type,(select sum(cnt) from gr_inventory_losslist  where warn_batch like '".$warn_batch."%')  as total from gr_inventory_losslist  where warn_batch like '".$warn_batch."%' group by loss_type";
        $typeRes=$model->query($typeSql);
        $this->reasonList=json_encode($reasonRes);
        $this->_reasonList=$reasonRes;
        $this->typeList=json_encode($typeRes);
        $this->_typeList=$typeRes;
        $this->display('lossproductdetail');
    }
    public function lossproductdetail($BIN="",$warn_batch="",$day=""){
        if(substr($BIN,0,1)=='1'&&$day==""){
            $where=" and BIN like '_".substr($BIN,1,4)."__' and warn_batch=$warn_batch";
        }else{
            $where=" and BIN=$BIN and warn_batch=$warn_batch";
        }
        if($day!=""){
            $startTime=strtotime($day."00:00:00");
            $endTime=strtotime($day."23:59:59");
            $where.=" and lossAt>=$startTime and lossAt <=$endTime";
        }
        $lossModel=new Model();
        $reasonSql="select reason ,sum(cnt) reasonCnt,(select sum(cnt) from gr_inventory_losslist where 1=1 $where ) as total from gr_inventory_losslist where 1=1 $where group by reason ";
        $reasonRes=$lossModel->query($reasonSql);
        $typeSql="select loss_type ,sum(cnt) typeCnt,(select sum(cnt) from gr_inventory_losslist where 1=1 $where ) as total from gr_inventory_losslist where 1=1 $where group by loss_type ";
        $typeRes=$lossModel->query($typeSql);
        $this->assign('typeList',json_encode($typeRes));
        $this->assign('_typeList',$typeRes);
        $this->assign('reasonList',json_encode($reasonRes));
        $this->assign('_reasonList',$reasonRes);
        $this->title="报损原因及方式占比";
        $this->display();
    }
    public function lossdaily($startDate="",$endDate=""){
        if($startDate==""){
            $current_month=date('Y-m',time());
            $startDate=$current_month."-01 00:00:00";
            $startTime=strtotime($startDate);
        }else{
            $startTime=strtotime($startDate);
        }
        if($endDate==""){
            $endDate=date('Y-m-d H:i:s',time());
            $endTime=strtotime($endDate);
        }else{
            $endTime=strtotime($endDate);

        }
        $where=" and lossAt>=$startTime and lossAt<=$endTime";
        $sql="select sum(cnt) losscnt,date_format(from_unixtime(lossAt),'%Y-%m-%d') as lossDate from gr_inventory_losslist where 1=1".$where." group by date_format(from_unixtime(lossAt),'%Y-%m-%d') order by date_format(from_unixtime(lossAt),'%Y-%m-%d') asc";
        $model=new Model();
        $res=$model->query($sql);
        $dayDataArr=[];
        $lossDataArr=[];
        foreach($res as $k=>$v){
            $dayDataArr[]=$v['lossDate'];
            $lossDataArr[]=$v['losscnt'];
        }
        $this->_list=$res;
        $this->startDate=$startDate;
        $this->endDate=$endDate;
        $this->dayDataArr=json_encode($dayDataArr);
        $this->lossDataArr=json_encode($lossDataArr);
        $this->title="日报损";
        $this->display();
    }
    public function dailydetail($day=""){
        $startTime=strtotime($day." 00:00:00");
        $endTime=strtotime($day." 23:59:59");
        $model=new Model();
        $typeSql="select loss_type ,sum(cnt) typeCnt,(select sum(cnt) from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime) as total from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime group by loss_type";
        $typeRes=$model->query($typeSql);
        $reasonSql="select reason ,sum(cnt) reasonCnt,(select sum(cnt) from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime) as total from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime group by reason";
        $reasonRes=$model->query($reasonSql);
        $this->_reasonList=$reasonRes;
        $this->_typeList=$typeRes;
        $this->reasonList=json_encode($reasonRes);
        $this->typeList=json_encode($typeRes);
        $this->title="日报损方式及原因比例[".date('Y年m月d日',strtotime($day))."]";
        $this->display();
    }
    public function dailyproductdetail($day=""){
        $startTime=strtotime($day." 00:00:00");
        $endTime=strtotime($day." 23:59:59");
        $model=new Model();
        $BINSql="select a.BIN,a.warn_batch,sum(a.cnt) cnt,b.pname,b.spec,
                (select sum(cnt) from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime) as total_loss
                from gr_inventory_losslist a,gr_material_product b where
                a.lossAt>=$startTime and a.lossAt<=$endTime and a.BIN=b.BIN
                group by BIN,warn_batch order by BIN asc";
        $BINRes=$model->query($BINSql);
        foreach($BINRes as $k=>$v){
            $BINRes[$k]['part']=$v['cnt']*100/$v['total_loss'];
        }
        $BINRes=multi_array_sort($BINRes,'part');
        $this->_list=$BINRes;
        $this->title="日报损水果占比统计[".date('Y年m月d日',strtotime($day))."]";
        $this->display();
    }
    public function dailyclassdetail($day=""){
        $startTime=strtotime($day." 00:00:00");
        $endTime=strtotime($day." 23:59:59");
        $model=new Model();
        $classSql="select LEFT(BIN,1) as class_no,sum(cnt) cnt,(select sum(cnt) from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime) as total from gr_inventory_losslist where lossAt>=$startTime and lossAt<=$endTime group by LEFT (BIN,1)";
        $classRes=$model->query($classSql);
        $this->classRes=json_encode($classRes);
        $this->_classRes=$classRes;
        $this->title="日报损货别占比统计[".date('Y年m月d日',strtotime($day))."]";
        $this->display();
    }
    public function losszjbatch(){
        $model=new Model();
        $zjSql="select LEFT (warn_batch,8) as batch,sum(cnt) as cnt from gr_inventory_losslist where BIN like '2%' group by left (warn_batch,8) order by warn_batch desc";
        $zsBatchRes=$model->query($zjSql);
        $total = count($zsBatchRes);
        if (isset($REQUEST['r'])) {
            $listRows = (int) $REQUEST['r'];
        } else {
            $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        }
        $page = new \Think\Page($total, $listRows, $REQUEST);
        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $this->assign('_total', $total);
        $zjSql.= " limit $page->firstRow,$page->listRows";
        $zsBatchRes=$model->query($zjSql);
        $this->_list=$zsBatchRes;
        foreach($zsBatchRes as $k=>$v){
            $sql="select (select ifnull(sum(b.cnt*a.spec),0) from gr_material_product a, gr_inventory_inlist b where a.BIN=b.BIN and b.warn_batch like '".$v['batch']."%' and a.BIN like '2%') as in_cnt,
                  (select ifnull(sum(a.spec*c.cnt),0) from gr_material_product a, gr_inventory_outlist c where a.BIN=c.BIN and c.warn_batch like '".$v['batch']."%' and a.BIN like '2%') as out_cnt,
                  (select ifnull(sum(c.cnt),0)  from gr_material_product a, gr_inventory_losslist c where a.BIN=c.BIN and c.warn_batch like '".$v['batch']."%' and a.BIN like '2%') as loss_cnt,
                  (select ifnull(sum(a.spec*d.cnt),0)  from gr_material_product a, gr_inventory_warn d where a.BIN=d.BIN and d.warn_batch like '".$v['batch']."%' and a.BIN like '2%') as inventory_cnt
                  from gr_material_product a limit 1";
            $totalRes=$model->query($sql);
            $zsBatchRes[$k]['in_cnt']=$totalRes[0]['in_cnt'];
            $zsBatchRes[$k]['out_cnt']=$totalRes[0]['out_cnt'];
            $zsBatchRes[$k]['loss_cnt']=$totalRes[0]['loss_cnt'];
            $zsBatchRes[$k]['inventory_cnt']=$totalRes[0]['inventory_cnt'];
            $zsBatchRes[$k]['loss_in']=number_format($totalRes[0]['loss_cnt']*100/$totalRes[0]['in_cnt']);
            $zsBatchRes[$k]['loss_out']=number_format($totalRes[0]['loss_cnt']*100/$totalRes[0]['out_cnt']);
        }
//        dump($zsBatchRes);
        $this->_list=$zsBatchRes;
        $this->title="成品报损统计";
        $this->display();

    }
    public function losszjproduct($batch=""){
        $sql="select a.*,sum(a.cnt) cnt,
            (select sum(a.cnt) from  gr_inventory_losslist a where a.warn_batch like '".$batch."%' and BIN like '2%') as total_loss,
            (select IFNULL(sum(b.spec*c.cnt),0) from gr_material_product b,gr_inventory_warn c where b.BIN=c.BIN and c.warn_batch=a.warn_batch and c.BIN=a.BIN ) as inventory_cnt,
            (select IFNULL(sum(b.spec*c.cnt),0) from gr_material_product b,gr_inventory_inlist c where b.BIN=c.BIN and c.warn_batch=a.warn_batch and c.BIN=a.BIN ) as in_cnt,
            (select IFNULL(sum(b.spec*c.cnt),0) from gr_material_product b,gr_inventory_outlist c where b.BIN=c.BIN and c.warn_batch=a.warn_batch and c.BIN=a.BIN ) as out_cnt
            from gr_inventory_losslist a where a.warn_batch like '".$batch."%'
            and a.BIN like '2%' group by a.BIN,a.warn_batch";
        $model=new Model();
        $res=$model->query($sql);
        foreach($res as $k=>$v){
            $res[$k]['loss_in']=number_format($v['cnt']*100/$v['in_cnt']);
            $res[$k]['loss_out']=number_format($v['cnt']*100/$v['out_cnt']);
            $res[$k]['part']=number_format($v['cnt']*100/$v['total_loss']);
        }
        $res=multi_array_sort($res,'part');
        $this->_list=$res;
        $this->title="成品报损明细[".date('Y-m-d',strtotime($batch))."期批次]";
        $this->display();
    }
    public function periodloss($startDate="",$endDate=""){
        if($endDate==""){
            $endDate=date("Y-m-d",time());
        }
        if($startDate==""){
            $startDate=$endDate;
        }
        $startTime=strtotime($startDate." 00:00:00");
        $endTime=strtotime($endDate." 23:59:59");
        $sql="select (select ifnull(sum(a.cnt),0) from gr_inventory_losslist a where a.lossAt<$endTime and a.lossAt>$startTime) as total_loss,
              (select ifnull(SUM(c.cnt*b.spec),0) from gr_inventory_inlist c,gr_material_product b where c.BIN=b.BIN and c.BIN like '1%' and c.createAt<$endTime and c.createAt>$startTime) as in_cnt,
               (select ifnull(SUM(d.cnt*b.spec),0) from gr_inventory_outlist d,gr_material_product b where d.BIN=b.BIN and d.BIN like '1%' and d.createAt<$endTime and d.createAt>$startTime) as out_cnt
                from gr_inventory_losslist limit 1;";
        $model=new Model();
        $res=$model->query($sql);
        foreach($res as $k=>$v){
            $res[$k]['loss_in']=number_format($v['total_loss']*100/$v['in_cnt']);
            $res[$k]['loss_out']=number_format($v['total_loss']*100/$v['out_cnt']);
        }
        $this->title="损耗统计";
        $this->_list=$res;
        $this->startDate=$startDate;
        $this->endDate=$endDate;
        $this->display();
    }
    public function periodreason($startDate="",$endDate=""){
        $startTime=strtotime($startDate." 00:00:00");
        $endTime=strtotime($endDate." 23:59:59");
        $model=new Model();
        $where=" and lossAt<=$endTime and lossAt>$startTime";
        $reasonSql="select reason,sum(cnt) reasonCnt,(select sum(cnt) from gr_inventory_losslist where 1=1 $where) as total from gr_inventory_losslist where 1=1 $where group by reason";
        $reasonRes=$model->query($reasonSql);
        $typeSql="select loss_type,sum(cnt) typeCnt,(select sum(cnt) from gr_inventory_losslist where 1=1 $where) as total from gr_inventory_losslist where 1=1 $where group by loss_type";
        $typeRes=$model->query($typeSql);
        $this->_reasonList=$reasonRes;
        $this->reasonList=json_encode($reasonRes);
        $this->_typeList=$typeRes;
        $this->typeList=json_encode($typeRes);
        $this->title="原因方式统计";
        $this->display('lossproductdetail');
    }
    public function periodproduct($startDate="",$endDate="",$export=0){
        $startTime=strtotime($startDate." 00:00:00");
        $endTime=strtotime($endDate." 23:59:59");
        $where=" and lossAt<=$endTime and lossAt>$startTime";
        $sql="select sum(cnt) as loss_cnt,BIN from gr_inventory_losslist where 1=1 $where and BIN like '1%' GROUP BY BIN";
        $model=new Model();
        $res=$model->query($sql);
        $data=[];
        foreach($res as $k=>$v){
            $subsql="select ".$v['BIN'].",pname,spec, (select ifnull(sum(a.cnt),0) from gr_inventory_losslist a where a.lossAt<$endTime and a.lossAt>$startTime and a.BIN like '_".substr($v['BIN'],1,4)."__') as total_loss,
                     (select ifnull(SUM(c.cnt*b.spec),0) from gr_inventory_inlist c,gr_material_product b where c.BIN=b.BIN and c.BIN=".$v['BIN']." and c.createAt<$endTime and c.createAt>$startTime) as in_cnt,
                     (select ifnull(SUM(d.cnt*b.spec),0) from gr_inventory_outlist d,gr_material_product b where d.BIN=b.BIN and d.BIN=".$v['BIN']." and d.createAt<$endTime and d.createAt>$startTime) as out_cnt
                     from gr_material_product where BIN=".$v['BIN']." limit 1 ";
            $model=new Model();
            $subRes=$model->query($subsql);
            $data[$k]['BIN']=$res[$k]['BIN'];
            $data[$k]['pname']=$subRes[0]['pname'];
            $data[$k]['spec']=$subRes[0]['spec'];
            $data[$k]['loss_cnt']=$subRes[0]['total_loss'];
            $data[$k]['in_cnt']=$subRes[0]['in_cnt'];
            $data[$k]['loss_in']=number_format($subRes[0]['total_loss']*100/$subRes[0]['in_cnt']);
            $data[$k]['out_cnt']=$subRes[0]['out_cnt'];
            $data[$k]['loss_out']=number_format($subRes[0]['total_loss']*100/$subRes[0]['out_cnt']);
        }
        $data=multi_array_sort($data,'loss_cnt');
        if($export==0){
            $this->_list=$data;
            $this->title="水果分类统计";
            $this->display();
        }else{
            if ($data){
                //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
                import("Org.Util.PHPExcel");
                import("Org.Util.PHPExcel.Writer.Excel5");
                import("Org.Util.PHPExcel.IOFactory.php");

                $filename = "output_order_excel";

                $headArr = array("BIN","商品名称","商品规格","报损数量","入库量","入库报损率","出库量","出库报损率");

                $title = $startDate."至".$endDate."报损数据";
                $this->getExcel($filename, $headArr, $data, $title);
            }else{
                $this->error("没有可导出的数据！");
            }
        }

    }
    public function periodclass($startDate="",$endDate=""){
        $startTime=strtotime($startDate." 00:00:00");
        $endTime=strtotime($endDate." 23:59:59");
        $where=" and lossAt<=$endTime and lossAt>$startTime";
        $sql="select sum(cnt) as cnt,left(BIN,1) as class_no,(select sum(cnt) from gr_inventory_losslist where 1=1 $where ) as total from gr_inventory_losslist where 1=1 $where group by left(BIN,1)";
        $model=new Model();
        $res=$model->query($sql);
        $this->_classRes=$res;
        $this->classRes=json_encode($res);
        $this->title="报损类型统计图";
        $this->display();
    }
    private function getExcel($fileName, $headArr, $data, $title) {
        //对数据进行检验
        if (empty($data) || !is_array($data)) {
            //die("data must be a array");
        }
        //检查文件名
        if (empty($fileName)) {
            exit;
        }
        // H:i:s
        $date = date("Y_m_d_H_i_s", time());
        //$date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";

        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //第一列设置报表时间范围
        $key = ord("A");
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $title);


        //第二列设置报表列头
        $key = ord("A");
        foreach ($headArr as $v) {
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '2', $v);
            $key += 1;
        }

        $column = 3;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach ($data as $key => $rows) { //行写入
            $span = ord("A");
            foreach ($rows as $keyName => $value) {// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j . $column, $value);
                $objActSheet->getColumnDimension('a')->setWidth(15);
                $objActSheet->getColumnDimension('b')->setWidth(10);
                $objActSheet->getColumnDimension('c')->setWidth(15);
                $objActSheet->getColumnDimension('d')->setWidth(20);
                $objActSheet->getColumnDimension('e')->setWidth(20);
                if (count($headArr) == 12){
                    $objActSheet->getColumnDimension('h')->setWidth(20);
                    $objActSheet->getColumnDimension('i')->setWidth(20);
                    $objActSheet->getColumnDimension('j')->setWidth(30);
                    $objActSheet->getColumnDimension('l')->setWidth(100);
                }else{
                    $objActSheet->getColumnDimension('g')->setWidth(20);
                    $objActSheet->getColumnDimension('h')->setWidth(20);
                    $objActSheet->getColumnDimension('i')->setWidth(30);
                    $objActSheet->getColumnDimension('j')->setWidth(30);
                    $objActSheet->getColumnDimension('k')->setWidth(100);
                }
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }

}

